package com.example.woke;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.diyview.Alertdialogall;
import com.woke.method.Checkpaypsw;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class CreditinfoActivity extends Activity implements OnClickListener {

	private Intent intent;
	private String id = "";
	// private int intid = 0;
	private TextView mTmoney, mTforwhy, mTdate, mTdiedate;
	private RelativeLayout mBar,mBar1;
	private MyApp application;
	private String user_id;
	private AlertDialog alertDialog;
	private String services_fee;
	private String user_login;//��������ϵ�绰
	private TextView mTphone;
	private String issee;//2Ϊδ�鿴��
	private RelativeLayout mRelativ;
	private ArrayList<String> list = new ArrayList<String>();
	private Alertdialogall zidingy;
//	private DialogWidget mDialogWidget;//����֧������

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_creditlist);

		intent = getIntent();
		id = intent.getStringExtra("id");
		user_id = intent.getStringExtra("user_id");// ������id
		application = (MyApp) getApplication();
		zidingy = new Alertdialogall();
		intview();

	}

	private void intview() {
		findViewById(R.id.avcreditl_image_back).setOnClickListener(this);
		mTmoney = (TextView) findViewById(R.id.avcreditl_text_money);
		mTdate = (TextView) findViewById(R.id.avcreditl_text_date);
		mTdiedate = (TextView) findViewById(R.id.avcreditl_text_diedate);
		mTforwhy = (TextView) findViewById(R.id.avcreditl_text_forwhy);
		mTphone = (TextView) findViewById(R.id.avcreditl_text_phoneno);
		mBar = (RelativeLayout) findViewById(R.id.avcreditlist_frmae);
		mBar1 = (RelativeLayout) findViewById(R.id.avcreditlist_frmae1);
		mRelativ = (RelativeLayout) findViewById(R.id.avcred_info_tel);
		mRelativ.setOnClickListener(this);
		findViewById(R.id.avcredit_relative_map).setOnClickListener(this);
		getcreditdatainfo(id);
	}

	private void getcreditdatainfo(String id) {
		AsyncHttpClient ahc = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "quota");
		params.put("a", "detailCredit");
		params.put("quota_id", id);
		params.put("userid", user_id);
		if (null == application.getDatas_load()) {
			params.put("currentid", "");
		} else {
			params.put("currentid", application.getDatas_load().getId());
//			Log.e("currensdsdtid"+id, "%%%%%"+application.getuser_id());
		}
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler() {
			private String quota_pic;
			private String quota_pic1;
			private String quota_pic2;

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
//				Log.e("���ý��", "tupoan" + response);
				try {
					String flag = response.getString("flag");
					if (flag.equals("success")) {
						JSONObject jsonObject = response.getJSONObject("data");
						JSONObject arra = jsonObject.getJSONObject("data");
						user_login = arra.getString("user_login");
						String status = arra.getString("status");
						issee = arra.getString("issee");
						Log.e("issee", issee+"看的");
						String repay_date = arra.getString("repay_date");
						mTdate.setText(repay_date + "年");
						String mark = arra.getString("mark");
						String type = arra.getString("type");
						
						quota_pic2 = arra.getString("quota_pic2");
						String creat_date = arra.getString("creat_date");
						quota_pic1 = arra.getString("quota_pic1");
						
						String id = arra.getString("id");
						String flag1 = arra.getString("flag");
						quota_pic = arra.getString("quota_pic");
						services_fee = arra.getString("services_fee");
						String interest = arra.getString("interest");
						String quota_use = arra.getString("quota_use");
						mTforwhy.setText(quota_use);
						String user_id = arra.getString("user_id");
						String publish = arra.getString("publish");
						String quota_money = arra.getString("quota_money");
						mTmoney.setText(quota_money + "元");
						String exp_date = arra.getString("exp_date");
						mTdiedate.setText(exp_date+"天");
						String friend = arra.getString("friend");
						String quota_type = arra.getString("quota_type");
						
						
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				list.add(""+quota_pic);
				list.add(""+quota_pic1);
				list.add(""+quota_pic2);
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				Toast.makeText(CreditinfoActivity.this, "数据失败！",
						Toast.LENGTH_SHORT).show();
//				Log.e("response", "ʧ��" + responseString);
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mRelativ.setClickable(false);
				mBar.setVisibility(View.VISIBLE);
			}

			@Override
			public void onFinish() {
				super.onFinish();
				mRelativ.setClickable(true);
				mBar.setVisibility(View.GONE);
				Log.e("response", "完成");
			}
		});
	}

	private void getcredit(String id) {
		AsyncHttpClient ahc = new AsyncHttpClient();
		// PersistentCookieStore myCookieStore = new
		// PersistentCookieStore(LoadActivity.this);
		// ahc.setCookieStore(myCookieStore);
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "quota");
		params.put("a", "checkCredit");
		params.put("userid", user_id);// ������id
		if(application.getDatas_load ()!=null){
		params.put("currentid", application.getDatas_load().getId());// �û�id
		}
		params.put("quotaid", id);// �ӵ�id
		params.put("services_fee", services_fee);
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler() {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("查看", "" + response);
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					if (flag.equals("success")) {
						issee = "1";
						aralogid("借款人电话", ""+user_login, 3);
//						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				Toast.makeText(CreditinfoActivity.this, "数据失败！",
						Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mBar1.setVisibility(View.VISIBLE);
			}

			@Override
			public void onFinish() {
				super.onFinish();
				mBar1.setVisibility(View.GONE);
				Log.e("response", "完成");
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(application.getDatas_load ()==null)return;
		if(null != application.getDatas_load().getId() && !application.getDatas_load().getId().equals("") && requestCode == 660){
			getcreditdatainfo(id);
		}else if(requestCode == 661 && resultCode == 600){
			boolean paypsw = data.getBooleanExtra("paypsw", false);
			if(paypsw == true){
				getcredit(id);
			}
		}
	}

	private void aralogid(String info, final String kll, final int ppp) {
		alertDialog = new AlertDialog.Builder(CreditinfoActivity.this)
				.setTitle(info)
				.setMessage(kll)
				.
				// setIcon(R.drawable.ic_launcher).
				setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// Intent intent = new Intent();
						if(ppp == 1){
							intent.setClass(CreditinfoActivity.this,
									LoadActivity.class);
							startActivityForResult(intent, 660);
							alertDialog.dismiss();
						}else if(ppp == 2){
							intent.setClass(CreditinfoActivity.this,CheckpaypswActivity.class);
							intent.putExtra("paymoney", services_fee);
							startActivityForResult(intent, 661);
							alertDialog.dismiss();
//							mDialogWidget=new DialogWidget(CreditinfoActivity.this, getDecorViewDialog());
//							mDialogWidget.show();
//							getcredit(id);
						}else{
							alertDialog.dismiss();
						}					
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						alertDialog.dismiss();
						// TODO Auto-generated method stub
					}
				}).create();
		alertDialog.show();
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.avcreditl_image_back:
			finish();
			break;
			
		case R.id.avcredit_relative_map:
			
			if(null == application.getDatas_load()){
				aralogid("还未登录", "前往登录？", 1);
			}else if(list.size() < 0){
				Toast.makeText(CreditinfoActivity.this, "用户未提交申请资料", Toast.LENGTH_SHORT).show();
			}else{
				Intent intent = new Intent(CreditinfoActivity.this,CreditmapActivity.class);
				intent.putExtra("id", id);
				Log.e("list", ""+list);
				startActivity(intent);
			}
			break;

		case R.id.avcred_info_tel:
			Checkpaypsw check = new Checkpaypsw();
			if (null == application.getDatas_load()) {
				aralogid("还未登录", "前往登录？", 1);
			}else if(check.check(application) == true){
				zidingy.aralogid(CreditinfoActivity.this, "还未设置支付密码","前往设置？");
			}else if (application.getDatas_load().getId().equals(user_id)) {
				Toast.makeText(CreditinfoActivity.this, "这是您自己发布的订单",
						Toast.LENGTH_SHORT).show();
			} else {
//				getcredit(id);
				if(issee.equals("2")){
					aralogid("查看联系方式将产生费用"+"￥"+services_fee, "确定查看？", 2);
				}else{
					aralogid("借款人电话", ""+user_login, 3);
				}
			}
			break;

		default:
			break;
		}
	}
}
